package cn.sciento.boot.tenant.hiam.domain.entity;

import io.choerodon.mybatis.annotation.ModifyAudit;
import io.choerodon.mybatis.annotation.VersionAudit;
import io.choerodon.mybatis.domain.AuditDomain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import cn.sciento.boot.platform.lov.annotation.LovValue;
import cn.sciento.boot.tenant.dto.TenantInitConfigDTO;
import cn.sciento.boot.tenant.hiam.domain.repository.TenantInitConfigRepository;
import org.springframework.beans.BeanUtils;

@ApiModel("租户初始化处理器配置")
@VersionAudit
@ModifyAudit
@Table(name = "hpfm_tenant_init_config")
public class TenantInitConfig extends AuditDomain {
  public static final String FIELD_TENANT_INIT_CONFIG_ID = "tenantInitConfigId";
  
  public static final String FIELD_SERVICE_NAME = "serviceName";
  
  public static final String FIELD_PROCESSOR_CODE = "processorCode";
  
  public static final String FIELD_PROCESSOR_NAME = "processorName";
  
  public static final String FIELD_PROCESSOR_TYPE = "processorType";
  
  public static final String FIELD_INIT_TYPE = "initType";
  
  public static final String FIELD_ORDER_SEQ = "orderSeq";
  
  public static final String FIELD_REMARK = "remark";
  
  @ApiModelProperty("表ID，主键，供其他表做外键")
  @Id
  @GeneratedValue
  private Long tenantInitConfigId;
  
  @ApiModelProperty("服务名称")
  @NotBlank
  private String serviceName;
  
  @ApiModelProperty("处理器代码")
  @NotBlank
  private String processorCode;
  
  @ApiModelProperty("处理器名称，表述处理器所做内容")
  @NotBlank
  private String processorName;
  
  @ApiModelProperty("处理器类型，代码HPFM.TENANT_PROCESSOR_TYPE，PRE_PROCESSOR-前置处理器，POST_PROCESSOR-后置处理器")
  @NotBlank
  @LovValue(lovCode = "HPFM.TENANT_PROCESSOR_TYPE", meaningField = "processorTypeMeaning")
  private String processorType;
  
  @ApiModelProperty("初始化类型，代码HPFM.TENANT_INIT_TYPE，CREATE-创建，UPDATE-更新")
  @NotBlank
  @LovValue(lovCode = "HPFM.TENANT_INIT_TYPE", meaningField = "initTypeMeaning")
  private String initType;
  
  @ApiModelProperty("排序号")
  @NotNull
  private Long orderSeq;
  
  @ApiModelProperty("备注说明")
  private String remark;
  
  @Transient
  private String processorTypeMeaning;
  
  @Transient
  private String initTypeMeaning;
  
  public Boolean isUpdate(TenantInitConfigRepository tenantInitConfigRepository) {
    TenantInitConfig query = new TenantInitConfig();
    query.setServiceName(this.serviceName);
    query.setProcessorCode(this.processorCode);
    TenantInitConfig tenantInitConfig = (TenantInitConfig)tenantInitConfigRepository.selectOne(query);
    if (tenantInitConfig != null) {
      setTenantInitConfigId(tenantInitConfig.getTenantInitConfigId());
      setObjectVersionNumber(tenantInitConfig.getObjectVersionNumber());
      return Boolean.valueOf(true);
    } 
    return Boolean.valueOf(false);
  }
  
  public static TenantInitConfig initInstance(TenantInitConfigDTO tenantInitConfigDTO) {
    TenantInitConfig tenantInitConfig = new TenantInitConfig();
    BeanUtils.copyProperties(tenantInitConfigDTO, tenantInitConfig);
    tenantInitConfig.setOrderSeq(Long.valueOf(tenantInitConfigDTO.getOrderSeq().longValue()));
    tenantInitConfig.setProcessorType(tenantInitConfigDTO.getProcessorType().name());
    tenantInitConfig.setInitType(tenantInitConfigDTO.getInitType().name());
    return tenantInitConfig;
  }
  
  public Long getTenantInitConfigId() {
    return this.tenantInitConfigId;
  }
  
  public void setTenantInitConfigId(Long tenantInitConfigId) {
    this.tenantInitConfigId = tenantInitConfigId;
  }
  
  public String getServiceName() {
    return this.serviceName;
  }
  
  public void setServiceName(String serviceName) {
    this.serviceName = serviceName;
  }
  
  public String getProcessorCode() {
    return this.processorCode;
  }
  
  public void setProcessorCode(String processorCode) {
    this.processorCode = processorCode;
  }
  
  public String getProcessorName() {
    return this.processorName;
  }
  
  public void setProcessorName(String processorName) {
    this.processorName = processorName;
  }
  
  public String getProcessorType() {
    return this.processorType;
  }
  
  public void setProcessorType(String processorType) {
    this.processorType = processorType;
  }
  
  public String getInitType() {
    return this.initType;
  }
  
  public void setInitType(String initType) {
    this.initType = initType;
  }
  
  public Long getOrderSeq() {
    return this.orderSeq;
  }
  
  public void setOrderSeq(Long orderSeq) {
    this.orderSeq = orderSeq;
  }
  
  public String getRemark() {
    return this.remark;
  }
  
  public void setRemark(String remark) {
    this.remark = remark;
  }
  
  public String getProcessorTypeMeaning() {
    return this.processorTypeMeaning;
  }
  
  public void setProcessorTypeMeaning(String processorTypeMeaning) {
    this.processorTypeMeaning = processorTypeMeaning;
  }
  
  public String getInitTypeMeaning() {
    return this.initTypeMeaning;
  }
  
  public void setInitTypeMeaning(String initTypeMeaning) {
    this.initTypeMeaning = initTypeMeaning;
  }
}
